package net.wanho.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import net.wanho.entity.User;
import net.wanho.mapper.UserMapper;
import net.wanho.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author：汤小洋
 * Date：2024-12-18 9:51
 * Description：<描述>
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, timeout = 30)
    @Override
    public PageInfo<User> page(int pageNum, int pageSize) {
        // 1.开启分页
        PageHelper.startPage(pageNum, pageSize);

        // 2.查询数据
        List<User> users = userMapper.selectAll();

        // 3.封装结果
        PageInfo<User> pageInfo = new PageInfo<>(users);

        return pageInfo;
    }


    @Override
    public void register(User user) {
        userMapper.insertUser(user);

        try {
            int i = 5/0;
        } catch (Exception e) {
            System.out.println("出异常啦");
        }
    }
}
